﻿package MV_BMP// ActionScript file
{
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	/**
	 *由矢量动画转换而成的位图动画类 
	 * @author 葛增文
	 * 
	 */	
	public class MovieClip_BMP extends Sprite
	{
		private var _framesAry:Array;
		private var _currentFrame:int;
		private var _currentLabel:String;
		private var _isStop:Boolean;
		public function MovieClip_BMP():void
		{
			this._framesAry=new Array;
			this.addListener();
		}
		
		/**
		 *转换成BMP动画 
		 * @param target
		 * 
		 */		
		public function turnFrom(target:MovieClip,excursionPoint:Point=null):void
		{
			for(var i:int=1;i<target.totalFrames+1;i++)
			{
				target.gotoAndStop(i);
				var turnSprite:Sprite=new Sprite();
				turnSprite.addChild(target);
				if(excursionPoint)
				{
					target.x=-excursionPoint.x;
					target.y=-excursionPoint.y;
				}
				var bmp:DisplayObject_BMP=new DisplayObject_BMP(turnSprite);
				var label:String=target.currentLabel;
				//trace("帧数="+i+",  标签="+label);
				this.framesAry.push({label:label,bmp:bmp,script:null});
			}
			this.currentFrame=1;
			this.stop();
		}
		
		/**
		 *添加监听 
		 * 
		 */		
		private function addListener():void
		{			
			this.addEventListener(Event.ENTER_FRAME,enterFrameHandle);
		}
		
		private function removeListener():void
		{
			this.removeEventListener(Event.ENTER_FRAME,enterFrameHandle);
		}
		
		
		/*模拟movieclip的方法*/
		public function play():void
		{
			this.isStop=false;
		}
		
		public function stop():void
		{
			this.isStop=true;
		}
		
		public function gotoAndPlay(frame:Object):void
		{
			var playFrame:int;
			if(frame is String)
			{
				playFrame=this.getFrameByLabel(frame as String);
				if(playFrame==-1)return;
			}
			else
			{
				playFrame=int(frame);
			}
			this.currentFrame=playFrame;
			this.play();
		}
		
		public function gotoAndStop(frame:Object):void
		{
			var playFrame:int;
			if(frame is String)
			{
				playFrame=this.getFrameByLabel(frame as String);
				if(playFrame==-1)return;
			}
			else
			{
				playFrame=int(frame);
			}
			this.currentFrame=playFrame;
			this.stop();
		}
		
		public function nextFrame():void
		{
			this.enterFrameHandle(null);
			this.stop();
		}
		
		public function prevFrame():void
		{
			if(this.currentFrame>0)
			{
				this.currentFrame-=1;
			}
			this.stop();
		}
		
		public function addFrameScript(...parameters):void
		{
			for(var i:int=0;i<parameters.length/2;i++)
			{
				var frame:int=parameters[i*2];
				var func:Function=parameters[i*2+1];
				if(frame<this.totalFrames)this.framesAry[frame].script=func;
			}
		}
		
		/**
		 *碰撞检测 
		 * @param checkObj
		 * @param checkObjBitmapDataPoint
		 * @return 
		 * 
		 */						
		public function hitTest(checkObj:Object,checkObjBitmapDataPoint:Point=null):Boolean
		{
			if(!this.stage)return false;
			var testTarget:Bitmap=this.getChildAt(0) as Bitmap;
			var firstPoint:Point=testTarget.parent.localToGlobal(new Point(testTarget.x,testTarget.y));
			if(checkObj is Point)
			{
				return testTarget.bitmapData.hitTest(firstPoint,1,checkObj);
			}
			else if(checkObj is Rectangle && checkObjBitmapDataPoint)
			{
				return testTarget.bitmapData.hitTest(firstPoint,1,(checkObj as Rectangle),checkObjBitmapDataPoint)
			}
			return false;
		}
		
		/**
		 *克隆方法 
		 * @return 
		 * 
		 */		
		public function clone():MovieClip_BMP
		{
			var returnMV:MovieClip_BMP=new MovieClip_BMP();
			for(var i:int=0;i<this.framesAry.length;i++)
			{
				var frameObj:Object=this.framesAry[i];
				var newBmp:Bitmap=new Bitmap((frameObj.bmp as Bitmap).bitmapData);
				var newFrameObj:Object={label:frameObj.label,bmp:newBmp,script:frameObj.script};
				returnMV.framesAry.push(newFrameObj);
			}
			returnMV.currentFrame=1;
			returnMV.stop();
			return returnMV;
		}
		
		/**
		 *根据帧标签放回帧数 
		 * @param label
		 * @return (如果找不到对应的帧数，返回-1)
		 * 
		 */		
		private function getFrameByLabel(label:String):int
		{
			for(var i:int=0;i<this.framesAry.length;i++)
			{
				var checkFrameObj:Object=this.framesAry[i];
				if(label==checkFrameObj.label)return (i+1);
			}
			return -1;
		}
		
		/**
		 *响应播放的事件
		 * @param e
		 * 
		 */		
		protected function enterFrameHandle(e:Event):void
		{
			if(this.isStop)return;
			if(this.currentFrame==this.totalFrames)
			{
				this.currentFrame=1;
			}
			else
			{
				this.currentFrame+=1;
			}
		}
		
		public function reset():void
		{
			while(this.numChildren)this.removeChildAt(0);
			this._framesAry=new Array;
			this.stop();
		}
		
		/**
		 *释放 
		 * 
		 */		
		public function release():void
		{
			this.removeListener();
			if(this.parent)this.parent.removeChild(this);
			while(this.numChildren)this.removeChildAt(0);
			this._framesAry=null;
		}
		
		/**
		 *获取帧数组 
		 * @return 
		 * 
		 */		
		private function get framesAry():Array
		{
			return _framesAry;
		}
		
		/**
		 *获取&设定当前播放帧 
		 * @return 
		 * 
		 */		
		public function get currentFrame():int
		{
			return _currentFrame;
		}
		
		public function set currentFrame(value:int):void
		{
			if(value<=this.totalFrames && value>0)
			{
				_currentFrame = value;
				while(this.numChildren)this.removeChildAt(0);
				var nowFrameObj:Object=this.framesAry[this.currentFrame-1];
				this.addChild(nowFrameObj.bmp as Bitmap);
				this.currentLabel=nowFrameObj.label;
				if(nowFrameObj.script)
				{
					nowFrameObj.script(this);
				}
				if(this.currentFrame==this.totalFrames)
				{
					this.dispatchEvent(new BMPMCEvent(BMPMCEvent.LAST_FRAME,null,true));
				}
			}
		}
		
		/**
		 *获取总帧数
		 * @return 
		 * 
		 */		
		public function get totalFrames():int
		{
			return this.framesAry.length;
		}
		
		/**
		 *获取&设定播放标记 
		 * @param value
		 * 
		 */		
		public function set isStop(value:Boolean):void
		{
			_isStop = value;
		}
		
		public function get isStop():Boolean
		{
			return _isStop;
		}
		
		/**
		 *获取&设定当前播放帧标签 
		 * @return (若没有,返回空)
		 * 
		 */		
		public function get currentLabel():String
		{
			return _currentLabel
		}
		
		public function set currentLabel(value:String):void
		{
			if(value!=this.currentLabel)
			{
				var foreLabel:String=this.currentLabel;
				this._currentLabel=value;
				this.dispatchEvent(new BMPMCEvent(BMPMCEvent.LABEL_CHANGE,{fore:foreLabel,now:this.currentLabel},true));
			}
		}
		
		
	}
}